package com.csu.service;

import com.csu.mapper.CartMapper;
import com.csu.model.CartItem;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("cartService")
@MapperScan("com.csu.mapper")
public class CartService {
    @Autowired
    private CartMapper cartDao;

    //添加商品进入购物车
    public void addToCart(String username, String itemId, int quantity) {
        CartItem cartItem = cartDao.getCartItem(username, itemId);
        if(cartItem == null){
            cartDao.insertToCart(username, itemId, quantity);
        }
        else{
            cartDao.updateCart(username, itemId, cartItem.getQuantity() + quantity);
        }
    }
    //修改购物车商品数量
    public void updateCart(String username, String itemId, int quantity) {
        cartDao.updateCart(username, itemId, quantity);
    }
    //删除购物车商品
    public void removeCart(String username, String itemId) {
        cartDao.deleteCart(username, itemId);
    }
    //获取购物车商品列表
    public List<CartItem> checkCartList(String username) {
        return cartDao.getCartListByUsername(username);
    }
    //获取购物车商品详情
    public CartItem getCartItem(String username, String itemId) {
        return cartDao.getCartItem(username, itemId);
    }
}
